package com.donggua.aichebao.commonui.home;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.donggua.aichebao.commonui.R;
import com.donggua.aichebao.commonui.TwoTextViewLayout;

/**
 * Created by Administrator on 2017-08-12.
 */
//com.donggua.aichebao.commonui.home.HomeTwoTextViewLayout
public class HomeTwoTextViewLayout extends RelativeLayout{
    protected static final int DEFAULT_SIZE = 15;
    protected static final int DEFAULT_TEXT_COLOR = 0Xff000000;
    protected int margin = 5;
    protected int textSizeType = TypedValue.COMPLEX_UNIT_PX;
    protected TextView tvFirst;
    protected TextView tvSecond;
    public HomeTwoTextViewLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomTwoTextView);
        margin = (int)context.getResources().getDimension(R.dimen.margin);
        init(context,ta);
        ta.recycle();

    }

    protected void init(Context context,TypedArray ta ){
        tvFirst =  getTextView(context);
        String firstText = ta.getString(R.styleable.CustomTwoTextView_firstText);
        int firstTextSize = (int)ta.getDimension(R.styleable.CustomTwoTextView_firstTextSize,DEFAULT_SIZE);
        int firstColor = ta.getColor(R.styleable.CustomTwoTextView_firstTextColor,DEFAULT_TEXT_COLOR);
        tvFirst.setText(firstText);
        tvFirst.setTextSize(textSizeType,firstTextSize);
        tvFirst.setTextColor(firstColor);
        Drawable drawable = ta.getDrawable(R.styleable.CustomTwoTextView_firstTextDrawable);
        setFirstDrawable(drawable);
        RelativeLayout.LayoutParams  firstLayoutParams = getMyLayoutParams();
        firstLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
        tvFirst.setLayoutParams(firstLayoutParams);
        this.addView(tvFirst);

        String secondText = ta.getString(R.styleable.CustomTwoTextView_secondText);
        int secondTextSize = (int)ta.getDimension(R.styleable.CustomTwoTextView_secondTextSize,DEFAULT_SIZE);
        int secondColor = ta.getColor(R.styleable.CustomTwoTextView_secondTextColor,DEFAULT_TEXT_COLOR);
        tvSecond =  getTextView(context);
        tvSecond.setText(secondText);
        tvSecond.setTextSize(textSizeType,secondTextSize);
        tvSecond.setTextColor(secondColor);
        RelativeLayout.LayoutParams  secondLayoutParams = getMyLayoutParams();
        secondLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        tvSecond.setLayoutParams(secondLayoutParams);
        this.addView(tvSecond);
    }

    private TextView getTextView(Context context) {
        TextView textView = new TextView(context);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(textSizeType,DEFAULT_SIZE);
        return textView;
    }

    protected void setFirstDrawable(Drawable drawable){
        if(drawable != null){
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());//必须设置图片大小，否则不显示
            tvFirst.setCompoundDrawables(drawable,null,null, null);
            tvFirst.setCompoundDrawablePadding(2*margin);
        }
    }
    protected  RelativeLayout.LayoutParams getMyLayoutParams(){
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        return layoutParams;
    }

    public void setSecondText(String text){
        tvSecond.setText(text);
    }

    public void setFirstText(String text){
        tvFirst.setText(text);
    }
}
